package com.push.xu.xpush.client;

import com.huawei.android.hms.agent.HMSAgent;
import com.huawei.android.hms.agent.push.handler.GetTokenHandler;
import com.huawei.hms.api.ConnectionResult;
import com.huawei.hms.api.HuaweiApiClient;
import com.huawei.hms.support.api.push.HuaweiPush;
import com.huawei.hms.support.api.push.TokenResult;
import com.push.xu.xpush.constant.Constant;
import com.push.xu.xpush.core.ZPushClient;
import com.push.xu.xpush.util.SPUtils;

import android.app.Application;
import android.app.NotificationManager;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

public class HMSPushClient implements ZPushClient {

	  private static final String TAG = "HMSPushClient";

	  private Context context;
	  private HuaweiApiClient huaweiApiClient;

	  @Override
	  public void initContext(Application application) {
	    this.context = application.getApplicationContext();
//	    HMSAgent.init(application);
//	    HMSAgent.connect((Activity) context, new ConnectHandler() {
//		    @Override
//		    public void onConnect(int rst) {
//		        Log.d("push","HMS connect end:" + rst);
//		    }
//		});  

	    huaweiApiClient = new HuaweiApiClient.Builder(context).addApi(HuaweiPush.PUSH_API)
				.addConnectionCallbacks(new HuaweiApiClient.ConnectionCallbacks() {
					@Override
					public void onConnected() {
						// 华为移动服务client连接成功，在这边处理业务自己的事件
						getToken();
						new Thread(new Runnable() {
							@Override
							public void run() {
								HuaweiPush.HuaweiPushApi.enableReceiveNormalMsg(huaweiApiClient, true);
								HuaweiPush.HuaweiPushApi.enableReceiveNotifyMsg(huaweiApiClient, true);
							}
						}).start();
					}

					@Override
					public void onConnectionSuspended(int i) {
						if (huaweiApiClient != null) {
							huaweiApiClient.connect();
						}
					}
				}).addOnConnectionFailedListener(new HuaweiApiClient.OnConnectionFailedListener() {
					@Override
					public void onConnectionFailed(ConnectionResult connectionResult) {
						Log.d("push","connect failure");
					}
				}).build();
	    
//		getToken();
		
	  }

	  private void getToken() {
		    HMSAgent.Push.getToken(new GetTokenHandler() {
		        @Override
		        public void onResult(int rtnCode, TokenResult tokenResult) {
		        	Log.d("push","get token: end" + rtnCode);
		        }
		    }); 
	  }

	  @Override
	  public void register() {
	    if (!huaweiApiClient.isConnected()) {
	      huaweiApiClient.connect();
	    }
	    getToken();
	  }

	  @Override
	  public void unRegister() {
	    //        huaweiApiClient.disconnect();
	    final String token = SPUtils.getInstance(Constant.PUSH_MESSAGE_TABLE).getString("push_token");
	    if (!TextUtils.isEmpty(token)) {
	      new Thread() {
	        @Override
	        public void run() {
	          super.run();
	          HuaweiPush.HuaweiPushApi.deleteToken(huaweiApiClient, token);
	          HuaweiPush.HuaweiPushApi.enableReceiveNormalMsg(huaweiApiClient, false);
	          HuaweiPush.HuaweiPushApi.enableReceiveNotifyMsg(huaweiApiClient, false);
	        }
	      }.start();
	    }
	  }

	@Override
	public void clearNotiById(Context context, int id) {
		NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
        manager.cancel(id); 
	}

	@Override
	public String getRegistration(Context context) {
		String token = SPUtils.getInstance(Constant.PUSH_MESSAGE_TABLE).getString("push_token");
//		String imei = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
		return token;
	}

	@Override
	public void bindAlias(String alias) {

	}

	@Override
	public void unBindAlias(String alias) {

	}

	@Override
	public void addTag(String tag) {

	}

	@Override
	public void deleteTag(String tag) {

	}
}
